package com.biz.primus.ms.task.config;


import lombok.Getter;
import lombok.Setter;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.Properties;

/**
 * 配置任务调度中心
 * [QRTZ_JOB_DETAILS], [QRTZ_TRIGGERS] and [QRTZ_CRON_TRIGGERS]
 * Created by david-liu on 2017/05/22 12:07.
 */
@Configuration
public class QuartzConfiguration {

    @Bean
    public Scheduler scheduler() throws IOException, SchedulerException {
        SchedulerFactory schedulerFactory = new StdSchedulerFactory(quartzProperties());
        Scheduler scheduler = schedulerFactory.getScheduler();
        scheduler.start();
        return scheduler;
    }

    /**
     * 设置quartz属性
     *
     * @throws IOException
     */
    public Properties quartzProperties() throws IOException {
        QuartzProperties quartzProperties = quartzConfigProperties();
        Properties prop = new Properties();
        prop.put("quartz.scheduler.instanceName", "ServerScheduler");
        prop.put("org.quartz.scheduler.instanceId", "AUTO");
        prop.put("org.quartz.scheduler.skipUpdateCheck", "true");
        prop.put("org.quartz.scheduler.jobFactory.class", "org.quartz.simpl.SimpleJobFactory");
        prop.put("org.quartz.jobStore.class", "org.quartz.impl.jdbcjobstore.JobStoreTX");
        prop.put("org.quartz.jobStore.driverDelegateClass", "org.quartz.impl.jdbcjobstore.StdJDBCDelegate");
        prop.put("org.quartz.jobStore.dataSource", "quartzDataSource");
        prop.put("org.quartz.jobStore.tablePrefix", "QRTZ_");
        prop.put("org.quartz.jobStore.isClustered", "false");
        prop.put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
        prop.put("org.quartz.threadPool.threadCount", "20");
        prop.put("org.quartz.dataSource.quartzDataSource.driver", quartzProperties.getJdbcDriver());
        prop.put("org.quartz.dataSource.quartzDataSource.URL", quartzProperties.getDatasourceUrl());
        prop.put("org.quartz.dataSource.quartzDataSource.user", quartzProperties.getDatasourceUsername());
        prop.put("org.quartz.dataSource.quartzDataSource.password", quartzProperties.getDatasourcePassword());
        prop.put("org.quartz.dataSource.quartzDataSource.validationQuery", "SELECT 1");
        prop.put("org.quartz.dataSource.quartzDataSource.validateOnCheckout", "true");
        prop.put("org.quartz.dataSource.quartzDataSource.maxConnections", "20");
        return prop;
    }

    @Bean
    @ConfigurationProperties("primus.quartz")
    QuartzProperties quartzConfigProperties(){return new QuartzProperties();}

    @Getter
    @Setter
    private static class QuartzProperties {
        private String jdbcDriver;
        private String datasourceUrl;
        private String datasourceUsername;
        private String datasourcePassword;
    }
}
